package com.jeasonzhao.report.model;

import com.jeasonzhao.commons.utils.Algorithms;

public class ObjectCollection extends java.util.Vector<Object>
{
    private static final long serialVersionUID = 1L;
    private static final String NULL_VALUE = "bdb9aba5-50ed-49c4-b2ea-e8298cf4c0ee=null";
    private static final String DELIMETER = "|*<&<SEP>&>*|";

    public ObjectCollection()
    {
    }

    public ObjectCollection(Object ...parameters)
    {
        if(null != parameters)
        {
            for(Object obj : parameters)
            {
                this.add(obj);
            }
        }
    }

    public int hashCode()
    {
        StringBuffer buf = new StringBuffer();
        for(Object obj : this)
        {
            buf.append(Algorithms.toString(obj));
            buf.append("--");
        }
        return buf.hashCode();
    }

    public boolean equals(Object obj)
    {
        if(obj == null || false == (obj instanceof ObjectCollection))
        {
            return false;
        }
        else
        {
            ObjectCollection otherColl = (ObjectCollection) obj;
            if(this.size() != otherColl.size())
            {
                return false;
            }
            else
            {
                return this.generateFakeKey().equals(otherColl.generateFakeKey());
            }
        }
    }

    private String generateFakeKey()
    {
        StringBuffer strRet = new StringBuffer();
        for(int n = 0;n < this.size();n++)
        {
            Object obj = this.elementAt(n);
            if(null == obj)
            {
                strRet.append(NULL_VALUE);
            }
            else
            {
                strRet.append("[" + obj.getClass().getName() + "]=");
                strRet.append(Algorithms.toString(obj));
            }
            strRet.append(DELIMETER);
        }
        return strRet.toString();
    }

    public String toString()
    {
        return this.toString(",",false);
    }

    public String toString(String str,boolean appendAtFirst)
    {
        StringBuffer strRet = new StringBuffer();
        for(int n = 0;n < this.size();n++)
        {
            if(str != null && (n > 0 || appendAtFirst))
            {
                strRet.append(str);
            }
            strRet.append(Algorithms.toString(this.elementAt(n)));
        }
        return strRet.toString();
    }

}
